<?php
/**
 * 给前台的接口
 */
class Frontdesk extends CI_Controller{

	//传入指定的类型名称，给一个数据
	function select_one(){
		$name = $_GET["name"]; //文章名称
		//查询该名称属于那个id号
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'classification',
			'where'=>[
				'name'=>$name
			]
		];
		$data = $this->news_model->select_form($cont);

		if (!isset($data[0])){
			return;
		}
		$id=$data[0]['id'];

		//查询一个文章传入前台
		$cont = [
			'table' => 'news',
			'where'=>[
				'category'=>$id
			],
			'limit' => [0, 1] //只要一个文章
		];
		$data = $this->news_model->select_form($cont);
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取老师
	function get_teachers(){
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'teacher',
			'order_by' => ['power' => 'DESC'],
			'limit' => [($page - 1) * $limit, $limit]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取新闻
	//获取老师
	function get_newss(){
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news',
			'order_by' => ['time' => 'DESC'],
			'limit' => [($page - 1) * $limit, $limit]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组

		$i = 0;
		foreach ($data as $da) {
			//获取转义时间戳
			$da['time'] = date('Y-m-d', $da['time']);
			$data[$i]['time'] = $da['time'];

			//获取转义文件分类
			$classification_id=$da['category'];
			$cont = [
				'table'=>'classification',
				'where'=>[
					'id'=>$classification_id
				]
			];
			$arrs = $this->news_model->select_form($cont);  //返回数据的数组
			$data[$i]['category_name']=$arrs[0]['name'];
			$i++;
		}

		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取指定id新闻
	function get_news_id(){
		$news_id = $_GET["news_id"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news',
			'where'=>[
				'id'=>$news_id
			]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		$i = 0;
		foreach ($data as $da) {
			//获取转义时间戳
			$da['time'] = date('Y-m-d', $da['time']);
			$data[$i]['time'] = $da['time'];

			//获取转义文件分类
			$classification_id=$da['category'];
			$cont = [
				'table'=>'classification',
				'where'=>[
					'id'=>$classification_id
				]
			];
			$arrs = $this->news_model->select_form($cont);  //返回数据的数组
			$data[$i]['category_name']=$arrs[0]['name'];
			$i++;
		}
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取指定id老师
	function get_teacher_id(){
		$teacher_id = $_GET["teacher_id"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'teacher',
			'where'=>[
				'id'=>$teacher_id
			]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	function news_count(){
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'news'
		];
		$data = $this->news_model->get_conunt($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	function teacher_count(){
		$this->load->model('server/news_model');
		$cont = [
			'table' => 'teacher'
		];
		$data = $this->news_model->get_conunt($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//根据上传表名来获取表数量
	function name_count(){
		$table = $_GET["table"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => $table
		];
		$data = $this->news_model->get_conunt($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取指定表老师
	function get_tables(){
		$table = $_GET["table"];
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => $table,
			'order_by' => ['power' => 'DESC'],
			'limit' => [($page - 1) * $limit, $limit]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取指定id 内容
	function get_tables_id(){
		$table = $_GET["table"];
		$id = $_GET["id"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => $table,
			'where'=>[
				'id'=>$id
			]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}

	//获取老师
	function get_table_arrs(){
		$table = $_GET["table"];
		$page = $_GET["page"];
		$limit = $_GET["limit"];
		$this->load->model('server/news_model');
		$cont = [
			'table' => $table,
			'order_by' => ['power' => 'DESC'],
			'limit' => [($page - 1) * $limit, $limit]
		];
		$data = $this->news_model->select_form($cont);  //返回数据的数组
		echo json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL;
	}
}
